package com.nuanshui.heatedloan.msgcenter.apppush.filter;

import com.beust.jcommander.internal.Lists;
import com.nuanshui.heatedloan.msgcenter.apppush.annotation.AppPushFilterProvider;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.AppPushResult;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.Invoker;
import com.nuanshui.heatedloan.msgcenter.model.AppPushMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;
import java.util.List;

/**
 * 异步执行过滤器
 * @param 
 * @author guohao
 * @date 2017年1月16日 下午4:12:34
 * @since $
 */
@AppPushFilterProvider(order=101,value="asyncFilter")
public class AsyncFilter implements AppPushFilter {

	@Autowired()
	private ThreadPoolTaskExecutor appPushExecutor;
	@Override
	public AppPushResult invoke(final Invoker invoker,final AppPushMessage appPushMessage) {
		
		List<HashMap<String,Object>> list= appPushMessage.getUserTokens();
//		if(list.size()>50) {//推送的用户量比较大采用异步推送
//			 for(HashMap<String,Object> map :list){
//				 while(appPushExecutor.getActiveCount()>=40){//防止出现拒绝策略
//				    try {
//						Thread.sleep(200);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				    final HashMap<String,Object> temp = map;
//				    appPushExecutor.execute(new Runnable() {
//						@Override
//						public void run() {
//							AppPushMessage target = new AppPushMessage();
//
//
//							//BeanUtils.copyProperties(appPushMessage,target);
//
//							List<HashMap<String,Object>>  l = Lists.newArrayList();
//							l.add(temp);
//							target.setUserTokens(l);
//							invoker.doInvoker(target);
//						}
//					});
//
//				 }
//			 }
//			 AppPushResult appPushResult = new AppPushResult();
//		     appPushResult.setAsync(true);
//		     return appPushResult;
//		}
		
		
		return invoker.doInvoker(appPushMessage);
	}

}
